package com.wkck.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkck.VO.CreateNoticeVo;
import com.wkck.VO.NoticeWithContentVo;
import com.wkck.VO.PageResult;
import com.wkck.VO.UploadContentVo;
import com.wkck.domain.News;
import com.wkck.domain.NewsContent;
import com.wkck.domain.Notice;
import com.wkck.domain.NoticeContent;
import com.wkck.dto.GetNoticeConditionsDto;
import com.wkck.result.Result;
import com.wkck.service.NoticeService;
import com.wkck.utils.DateTimeGeneration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: CK
 * @Date: 2024/03/25/13:03
 * @Description:
 */
@RestController
@RequestMapping("/noticeBg")
@Api(value = "公告管理",tags = "公告管理")
@CrossOrigin
@Slf4j
public class NoticeBgController {

    @Autowired
    private NoticeService noticeService;

    /***
    * @Author: CK
    * @Date: 2024/3/25
    * @Param: []
    * @Description: 创建通知
    */
    @GetMapping("/create-notice")
    @ApiOperation(value = "创建通知")
    public Result<CreateNoticeVo> createNotice() {
        log.info("创建通知时间: {}", DateTimeGeneration.now());
        return Result.success(noticeService.createNotice());
    }

    /***
    * @Author: CK
    * @Date: 2024/3/25
    * @Param: [id]
    * @Description: 根据id获得通知信息
    */
    @GetMapping("/get-notice/{id}")
    @ApiOperation(value = "根据id获得通知信息")
    public Result<NoticeWithContentVo> getNoticeAllById(@PathVariable Long id) {
        log.info("根据id获得通知信息 ===> {}", id);
        return Result.success(noticeService.getNoticeAllById(id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/25
    * @Param: [notice]
    * @Description: 修改通知信息
    */
    @PostMapping("/update-notice")
    @ApiOperation(value = "修改通知信息")
    public Result<Boolean> updateNotice(@RequestBody NoticeWithContentVo notice) {
        log.info("需要修改的通知信息: {}", notice);
        return Result.success(noticeService.updateNotice(notice));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/26
    * @Param: [dto]
    * @Description: 查询所有通知数据
    */
    @PostMapping("/get-notices")
    @ApiOperation(value = "查询所有通知数据")
    public Result<PageResult> getNotices(@RequestBody GetNoticeConditionsDto dto) {
        log.info("查询所有通知参数: {}", dto);
        return Result.success(noticeService.getNotices(dto));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/26
    * @Param: [ids]
    * @Description: 根据ids删除通知
    */
    @DeleteMapping("/remove-notices")
    @ApiOperation(value = "根据ids删除通知")
    public Result<Boolean> removeNoticesByIds(@RequestBody Map<String, List<String>> ids) {
        log.info("需要删除的新闻id ===> {}", ids.get("ids"));
        return Result.success(noticeService.removeNoticesByIds(ids.get("ids")));
    }

    /****
     * @Author: CK
     * @Date: 2024/3/23
     * @Param: [notice]
     * @Description: 修改新闻状态
     */
    @PutMapping("/change-state")
    @ApiOperation(value = "修改公告或通知状态")
    public Result<Boolean> changeState(@RequestBody Notice notice) {
        log.info("需要修改公告或通知的状态 : {}", notice);
        return Result.success(noticeService.changeState(notice));
    }

    /***
     * @Author: CK
     * @Date: 2024/3/23
     * @Param: [id]
     * @Description: 预览新闻
     */
    @GetMapping("/preview-content/{id}")
    @ApiOperation(value = "预览公告或通知")
    public Result<NoticeContent> getPreviewNewsContent(@PathVariable Long id) {
        log.info("预览公告或通知id ===> {}", id);
        return Result.success(noticeService.getPreviewNoticeContent(id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/26
    * @Param: [file, id]
    * @Description: 上传正文中图片
    */
    @PostMapping("/content-photo/{id}")
    @ApiOperation(value = "上传正文中图片")
    public Result<UploadContentVo> uploadContentPhoto(@RequestPart MultipartFile file, @PathVariable("id") Long id) throws IOException {
        log.info("需要上传的正文 ===> {}, 图片: {}", id, file);

        File tempFile = File.createTempFile("minio_notice_content_photo", ".temp");
        file.transferTo(tempFile);

        String localFilePath = tempFile.getAbsolutePath();

        long size = file.getSize();

        String filename = file.getOriginalFilename();

        return Result.success(noticeService.uploadContentPhoto(localFilePath, size, filename, id));
    }
}
